package com.zhku.meeting.interceptor;

import com.zhku.meeting.Annotation.loginCharacter;
import com.zhku.meeting.util.ResultUtil;
import com.zhku.meeting.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Objects;

@Component
@Slf4j
public class TokenInterceptor implements HandlerInterceptor {
    private Long l = System.currentTimeMillis();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getMethod().equals("OPTIONS")) {
            response.setStatus(HttpServletResponse.SC_OK);
            return true;
        }

        //获取请求的角色注解
        if (!(handler instanceof HandlerMethod)) {
            ResultUtil.returnNoLogin(response, HttpStatus.NOT_FOUND.value(), HttpStatus.NOT_FOUND.getReasonPhrase(), "页面不存在");
            return false;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        loginCharacter character = method.getAnnotation(loginCharacter.class);


        response.setCharacterEncoding("UTF-8");

        String token = request.getHeader("Authorization");
        if (token != null) {
            boolean result = TokenUtil.verify(token);
            //确认登录状态检查当前角色是否有请求的目标方法的权限
            if (result) {
                if (Objects.isNull(character)) {  //没有注解的方法直接放行
                    return true;
                }
                String name = character.name();
                String checkToken = TokenUtil.getNo(token);
                if ((checkToken.equals("admin") && name.equals("admin")) || (!checkToken.equals("admin") && name.equals("user"))) {
                    System.out.println("获取token，权限正确,拦截器放行");
                    return true;
                }
            }
        }
        log.info("被拦截器拦截");
        ResultUtil.returnNoLogin(response, HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase(), "token过期，请重新登录");
//        response.setCharacterEncoding("utf-8");
//        response.setContentType("application/json; charset=utf-8");
//        response.addHeader("message", "token error");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
        if ((handler instanceof HandlerMethod)) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            if (log.isDebugEnabled()) {
                log.debug("The Method:{}", method.getName());
                log.debug("请求执行时间:{}", System.currentTimeMillis() - l);
            }
        }

    }
}
